package com.peng.jackson;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectReader;
import com.peng.utils.JacksonUtil;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * JacksonMargeSample
 * 使用 jackson marge 更新属性
 * 只会进行浅拷贝，并不会递归的合并属相值
 *
 * @author lupeng
 * Created on 2022-04-27
 */
public class JacksonMargeSample {

    private static final Logger logger = LoggerFactory.getLogger(JacksonMargeSample.class);

    @Data
    @AllArgsConstructor
    static class Bean1 {
        private String name;
        private Bean2 bean2;
    }

    @Data
    @AllArgsConstructor
    static class Bean2 {
        private String fname;
        private String lname;
    }

    @Test
    public void s2() {
        try {
            Bean2 bean2 = new Bean2("zhang", "san");
            Bean1 beijing = new Bean1("beijing", bean2);
            ObjectReader objectReader = JacksonUtil.getObjectMapper().readerForUpdating(beijing);
            Bean1 marged = objectReader.readValue("""
                    {
                        "name": "suzhou",
                        "fname": {
                            "lname": "gouzi"
                        }
                    }
                    """);
            System.out.println(marged);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

}
